﻿using Cinch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeSHARPer.Services
{
    public class ServiceResolver
    {
        #region Singleton
        private static volatile ServiceResolver instance;
        private static object syncRoot = new Object();

        #region constructor
        /// <summary>
        /// Constructor
        /// </summary>
        private ServiceResolver()
        {
            this.MessageBoxService = MEFedMVVM.ViewModelLocator.ViewModelRepository.Instance.Resolver.Container.GetExport<IMessageBoxService>().Value;
            this.OpenFileService = MEFedMVVM.ViewModelLocator.ViewModelRepository.Instance.Resolver.Container.GetExport<IOpenFileService>().Value;
            this.SaveFileService = MEFedMVVM.ViewModelLocator.ViewModelRepository.Instance.Resolver.Container.GetExport<ISaveFileService>().Value;
        }
        #endregion constructor

        public static ServiceResolver Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new ServiceResolver();
                    }
                }

                return instance;
            }
        }
        #endregion Sigleton

        IMessageBoxService messageBoxService;
        IOpenFileService openFileService;
        ISaveFileService saveFileService;
        IUIVisualizerService uiVisualizerService;

        public IMessageBoxService MessageBoxService
        {
            get
            {
                if (messageBoxService == null)
                    messageBoxService = GetService<IMessageBoxService>();
                return messageBoxService;
            }
            private set { messageBoxService = value ;}
        }

        public IOpenFileService OpenFileService
        {
            get
            {
                if (openFileService == null)
                    openFileService = GetService<IOpenFileService>();
                return openFileService;
            }
            private set { openFileService = value; }
        }

        public ISaveFileService SaveFileService
        {
            get
            {
                if (saveFileService == null)
                    saveFileService = GetService<ISaveFileService>();
                return saveFileService;
            }
            private set { saveFileService = value; }
        }

        public IUIVisualizerService UIVisualizerService
        {
            get
            {
                if (uiVisualizerService == null)
                    uiVisualizerService = GetService<IUIVisualizerService>();
                return uiVisualizerService;
            }
            private set { uiVisualizerService = value; }
        }

        private T GetService<T>()
        {
            return MEFedMVVM.ViewModelLocator.ViewModelRepository.Instance.Resolver.Container.GetExport<T>().Value;
        }

        public System.Windows.Window MainWindow { get; set; }
    }
}
